function A() {
  return new Promise((resolve, reject) => {
    resolve(1)
  })
}

function B() {
  return new Promise((resolve, reject) => {
    resolve(2)
  })
}

function C() {
  return new Promise((resolve, reject) => {
    resolve(3)
    // reject(3) // 一旦进入 reject,如果设置有 catch，将会被 catch 捕获，不会进需进行下面的链式调用了
  })
}

function D() {
  return new Promise((resolve, reject) => {
    resolve(4)
  })
}

A()
  .then(res => {
    console.log(res)
    return B()
  })
  .then(res => {
    console.log(res)
    return C()
  })
  .then(res => {
    console.log(res)
    return D()
  })
  .then(res => {
    console.log(res)
    return {
      then: function (cb) {
        cb('test value')
      }
    }
  })
  .then(res => {
    console.log(res) // test value
  })
  .then(res => {
    console.log(res) // undefined
  })
  .catch(err => {
    console.log('err', err)
  })